package edu.java.texbooks.scjp.collections.test06.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.java.texbooks.scjp.collections.test06.iterator.fruit.Fruit;
import edu.java.texbooks.scjp.collections.test06.iterator.fruit.FruitSort;

public class TestIterator {
	static Fruit pear = new Fruit("pear", FruitSort.SEED);
	static Fruit apple = new Fruit("apple", FruitSort.SEED);
	static Fruit strawberry = new Fruit("strawberry", FruitSort.OTHER);
	static Fruit peach = new Fruit("peach", FruitSort.PIT);

	public static void main(String[] args) {
		// initialize list of fruits
		List<Fruit> fruits = new ArrayList<Fruit>();
		fruits.add(pear);
		fruits.add(apple);
		fruits.add(strawberry);
		fruits.add(peach);
		fruits.add(peach);

		// test Iterator with Generic
		// Iterator --> hasNext(), next(), remove(). Special notes for remove()!
		test01(fruits);

		// test Iterator without Generic
		test02(fruits);

	}

	public static void testFruit() {

	}

	public static void test01(List<Fruit> fruits) {

		System.out.println("\n" + "fruits: " + fruits);
		System.out.println("\n\n");

		// Iterator
		Iterator<Fruit> iteratorFruits = fruits.iterator();

		if (iteratorFruits.hasNext()) {
			Fruit someFruit = iteratorFruits.next();
			System.out.println("eat: " + someFruit);
			iteratorFruits.remove();

			// throw exception
			// iteratorFruits.remove();
		}
		System.out.println("\n" + fruits);
	}

	public static void test02(List<Fruit> fruits) {

		System.out.println("\n" + "fruits: " + fruits);
		System.out.println("\n\n");

		// Iterator
		Iterator iterator = fruits.iterator();
		if (iterator.hasNext()) {
			// Object fruit= iterator.next(); // it is absolutely true and works

			Fruit fruit = (Fruit) iterator.next(); // explicit cast
			System.out.println("eat: " + fruit);

			iterator.remove();
		}

		System.out.println("\n" + fruits);
	}

}
